package org.seven.jrdp.biz.sys.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.seven.jrdp.biz.sys.mapper.SysOrganMapper;
import org.seven.jrdp.biz.sys.mapper.SysUserMapper;
import org.seven.jrdp.biz.sys.model.SysOrgan;
import org.seven.jrdp.biz.sys.model.SysUser;
import org.seven.jrdp.commons.constant.GlobalContants;
import org.seven.jrdp.commons.exception.BizException;
import org.seven.jrdp.commons.model.TreeNode;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@CacheConfig(cacheNames = "organ")
@Service
public class SysOrganService {
	@Autowired
	private SysOrganMapper sysOrganMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	@Transactional(readOnly = true)
	public List<SysOrgan> find() {
		Example example = new Example(SysOrgan.class);
		example.orderBy("code");
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleteFlag", "0");
		return sysOrganMapper.selectByExample(example);
	}

	@Transactional(readOnly = true)
	public SysOrgan get(String id) {
		return sysOrganMapper.selectByPrimaryKey(id);
	}

	@CacheEvict(cacheNames = { "organ", "user", "mgt" }, allEntries = true)
	@Transactional
	public void insert(SysOrgan sysOrgan) {
		sysOrganMapper.insertSelective(sysOrgan);
	}

	@CacheEvict(cacheNames = { "organ", "user", "mgt" }, allEntries = true)
	@Transactional
	public void update(SysOrgan sysOrgan) {
		sysOrganMapper.updateByPrimaryKeySelective(sysOrgan);
	}

	@CacheEvict(cacheNames = { "organ", "user", "mgt" }, allEntries = true)
	@Transactional
	public void delete(String id) {
		if (GlobalContants.ORGAN.equals(id)) {
			throw new BizException("不允许删除总机构");
		}
		{
			Example example = new Example(SysOrgan.class);
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("pid", id);
			criteria.andEqualTo("deleteFlag", "0");
			if (sysOrganMapper.selectCountByExample(example) > 0) {
				throw new BizException("该机构下存在子机构，不允许删除");
			}
		}
		{
			Example example = new Example(SysUser.class);
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("organId", id);
			criteria.andEqualTo("deleteFlag", "0");
			if (sysUserMapper.selectCountByExample(example) > 0) {
				throw new BizException("该机构下存在用户，不允许删除");
			}
		}
		SysOrgan sysOrgan = new SysOrgan();
		sysOrgan.setId(id);
		sysOrgan.setDeleteFlag("1");
		sysOrganMapper.updateByPrimaryKeySelective(sysOrgan);
	}

	@Cacheable(key = "'organ_tree'")
	@Transactional(readOnly = true)
	public List<TreeNode> findTree() {
		return sysOrganMapper.findTree();
	}

	@Cacheable(key = "'organ_tree_'+#code")
	@Transactional(readOnly = true)
	public List<TreeNode> findTreeByCode(String code) {
		return sysOrganMapper.findTreeByCode(code);
	}

	@Transactional(readOnly = true)
	public void addOrganCode(Map<String, Object> map, String code) {
		String id = (String) map.get(GlobalContants.SEARCH_ORGAN_ID);
		if (id != null && !"".equals(id)) {
			SysOrgan sysOrgan = sysOrganMapper.selectByPrimaryKey(id);
			if (sysOrgan != null) {
				code = sysOrgan.getCode();
			}
		}
		map.put(GlobalContants.SEARCH_ORGAN_CODE, code + "%");
	}
}